﻿<%@ WebHandler Language="C#" Class="cheliangyunshu" %>

using System;
using System.Web;
using System.Data;
using KeaoBase;
using ServiceStack.Redis;
using Newtonsoft.Json;
using System.Configuration;
using System.Collections.Generic;

public class cheliangyunshu : IHttpHandler {

    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";
        try
        {
            string uid = context.Request.QueryString["uid"];
            string kid = context.Request.QueryString["kid"];
            if (!Auth.UserLogined(uid, kid))
            {
                context.Response.Write("[]");
                return;
            }
            string key = KeaoUtils.GetMD5Str(context.Request.Url.OriginalString.Replace(uid,"cache").Replace(kid,"cache"));
            using (IRedisClient redis = RedisClientPool.GetReadOnlyRedisClient())
            {
                if (redis != null)
                {
                    if (redis.ContainsKey(key))
                    {
                        context.Response.Write(redis.GetValue(key));
                        return;
                    }
                }
            }
            string zhanDian = context.Request.QueryString["zhandian"];
            string kaiShiShiJian = context.Request.QueryString["kaishishijian"];
            string jieShuShiJian = context.Request.QueryString["jieshushijian"];
            if (string.IsNullOrEmpty(zhanDian))
            {
                context.Response.Write("[]");
                return;
            }
            BaseDataBase m_DB = System.Reflection.Assembly.Load("KeaoDataBase").CreateInstance(string.Format("KeaoDataBase.{0}", ConfigurationManager.AppSettings["数据库类型"])) as BaseDataBase;
            if (!m_DB.InitDataBase(ConfigurationManager.ConnectionStrings["DataSource"].ConnectionString))
            {
                context.Response.Write("[]");
                return;
            }
            else
            {
                string createCondition = string.Empty;
                if (zhanDian != "—全部—")
                {
                    createCondition += m_DB.CreateAndCondition("zhandian", DataValueCommonType.字符串, zhanDian, "", SearchConditionType.等于);
                }
                if (!string.IsNullOrEmpty(kaiShiShiJian) && !string.IsNullOrEmpty(jieShuShiJian))
                {
                    createCondition += m_DB.CreateAndCondition("pizhongshijian", DataValueCommonType.日期时间,string.Format("{0} 00:00:00", kaiShiShiJian),string.Format("{0} 23:59:59",jieShuShiJian), SearchConditionType.between);
                }
                Dictionary<string, string> orderBy = new Dictionary<string, string>();
                orderBy.Add(m_DB.DateFormatFun("pizhongshijian"), "desc");
                DataTable Dt = m_DB.GetDataTableByCondition("tbl_chengzhong",string.Format("{0} as riqi,chepaihao,count(id) as yunshucishu",m_DB.DateFormatFun("pizhongshijian")),null,createCondition,string.Format("{0},chepaihao",m_DB.DateFormatFun("pizhongshijian")),orderBy);
                if (Dt.Rows.Count > 0)
                {
                    string dtJson = JsonConvert.SerializeObject(Dt);
                    using (IRedisClient redis = RedisClientPool.GetRedisClient())
                    {
                        if (redis != null)
                        {
                            redis.SetValue(key, dtJson, TimeSpan.FromMinutes(10.0));
                        }
                    }
                    context.Response.Write(dtJson);
                }
                else
                {
                    context.Response.Write("[]");
                }
            }
        }
        catch (Exception err)
        {
            KeaoUtils.LogTxt(string.Format("查询车辆运输异常。{0}", err.Message));
        }
    }

    public bool IsReusable {
        get {
            return false;
        }
    }

}